#include "gdt.h"
#include "tss.h"

int gdt_index = 6;

gdt_entry gdt[GDT_COUNT] = {
	(gdt_entry){0x0000,0x0000,0x00,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x00},	//DESCRIPTOR NULO.
	(gdt_entry){0xFFFF,0x0000,0x00,0xA,0x1,0x0,0x1,0xF,0x0,0x0,0x1,0x1,0x00}, 	//CODIGOS, NIVEL: 0(kernel).
	(gdt_entry){0xFFFF,0x0000,0x00,0x2,0x1,0x0,0x1,0xF,0x0,0x0,0x1,0x1,0x00},	//DATOS, NIVEL: 0(kernel).
	(gdt_entry){0x0F9F,0x8000,0x0B,0x2,0x1,0x0,0x1,0x0,0x0,0x0,0x1,0x0,0x00},	//SOLO MEM DE VIDEO.(datos)
	(gdt_entry){0xFFFF,0x0000,0x00,0xA,0x1,0x3,0x1,0xF,0x0,0x0,0x1,0x1,0x00},	//codigos-> CODIGOS, NIVEL: 3(usuario).
	(gdt_entry){0xFFFF,0x0000,0x00,0x2,0x1,0x3,0x1,0xF,0x0,0x0,0x1,0x1,0x00}	//datos -> DATOS, NIVEL: 3(usuario).
};

void nueva_entrada_gdt(gdt_entry entrada){
	gdt[gdt_index] = entrada;
	gdt_index++;
}

gdt_descriptor GDT_DESC = {sizeof(gdt)-1, (unsigned int)&gdt};


/*INDICE 1--> Codigos, Nivel 0
	unsigned short limit_0_15		= 0xFFFF
	unsigned short base_0_15		= 0x0000
	unsigned char base_23_16		= 0x00
	unsigned char type:4(INCLUYE A) = 1010b									(codigo)(c=conforming=0)(r=readable=1)(A=accesed=0)
	unsigned char s:1		        = 1b									(de memoria)
	unsigned char dpl:2		        = 00b									(00 = privilegio de kernel)
	unsigned char p:1		        = 1b
	unsigned char limit_16_19:4     = 0xF
	unsigned char avl:1				= 0b									(available for use by system software)
	unsigned char l:1				= 0b									(seteado en 0=segmento de codigo en 32bits(1=64b))
	unsigned char db:1				= 1b									(32bits)
	unsigned char g:1				= 1b 									(granuralidad = 4k)
	unsigned char base_31_24		= 0x00
*/

/*INDICE 2--> datos, Nivel 0
	unsigned short limit_0_15		= 0xFFFF
	unsigned short base_0_15		= 0x0000
	unsigned char base_23_16		= 0x00
	unsigned char type:4(INCLUYE A) = 0010b									(datos)(e=expand-down=0)(w=writable=1)(A=accesed=0)
	unsigned char s:1		        = 1b 									(de memoria)
	unsigned char dpl:2		        = 00b									(00 = privilegio de kernel)
	unsigned char p:1		        = 1b
	unsigned char limit_16_19:4     = 0xF
	unsigned char avl:1				= 0b									(available for use by system software)
	unsigned char l:1				= 0b									(seteado en 0=segmento de codigo en 32bits(1=64b))
	unsigned char db:1				= 1b									(32bits)
	unsigned char g:1				= 1b 									(granuralidad = 4k)
	unsigned char base_31_24		= 0x00
*/

/*INDICE 3--> Solo mem de video
	unsigned short limit_0_15		= 0x0FA0
	unsigned short base_0_15		= 0x8000
	unsigned char base_23_16		= 0x0B
	unsigned char type:4(INCLUYE A) = 0010b									(datos)(e=expand-down)(w=writable)(A=accesed=0)
	unsigned char s:1		        = 1b 									(de memoria)
	unsigned char dpl:2		        = 00b									(00 = privilegio de kernel)
	unsigned char p:1		        = 1b
	unsigned char limit_16_19:4     = 0x0
	unsigned char avl:1				= 0b									(available for use by system software)
	unsigned char l:1				= 0b									(seteado en 0=segmento de codigo en 32bits(1=64b))
	unsigned char db:1				= 1b									(32bits)
	unsigned char g:1				= 0 									(granuralidad = byte)
	unsigned char base_31_24		= 0x00
*/

/*INDICE 4--> Codigos, Nivel 3
	unsigned short limit_0_15		= 0xFFFF
	unsigned short base_0_15		= 0x0000
	unsigned char base_23_16		= 0x00
	unsigned char type:4(INCLUYE A) = 1010b									(codigo)(c=conforming=0)(r=readable=1)(A=accesed=0)
	unsigned char s:1		        = 1b 									(de memoria)				
	unsigned char dpl:2		        = 11b									(11 = privilegio de usuario)
	unsigned char p:1		        = 1b
	unsigned char limit_16_19:4     = 0xF
	unsigned char avl:1				= 0b									(available for use by system software)
	unsigned char l:1				= 0b									(seteado en 0=segmento de codigo en 32bits(1=64b))
	unsigned char db:1				= 1b									(32bits)
	unsigned char g:1				= 1b 									(granuralidad = 4k)
	unsigned char base_31_24		= 0x00
*/

/*INDICE 5--> datos, Nivel 3
	unsigned short limit_0_15		= 0xFFFF
	unsigned short base_0_15		= 0x0000
	unsigned char base_23_16		= 0x00
	unsigned char type:4(INCLUYE A) = 0010b									(datos)(e=expand-down=0)(w=writable=1)(A=accesed=0)
	unsigned char s:1		        = 1b 									(de memoria)
	unsigned char dpl:2		        = 11b									(11 = privilegio de usuario)
	unsigned char p:1		        = 1b
	unsigned char limit_16_19:4     = 0xF
	unsigned char avl:1				= 0b									(available for use by system software)
	unsigned char l:1				= 0b									(seteado en 0=segmento de codigo en 32bits(1=64b))
	unsigned char db:1				= 1b									(32bits)
	unsigned char g:1				= 1b 									(granuralidad = 4k)
	unsigned char base_31_24		= 0x00
*/
